package com.warski.wonderfires.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuhaiping on 2020/8/29.
 */
public class FileUtils {
    private static Context context;

    public static void init(Context context) {
        FileUtils.context = context;
    }

    /**
     * 判断url是不是asset文件
     * 定了个规则，如果文件是在asset中的，那么url前面加上ASSET:
     *
     * @param url
     * @return
     */
    public static boolean isAssetFile(String url) {
        return url.startsWith("ASSET:");
    }

    /**
     * 获取文件名
     *
     * @return
     */
    public static String getAssetFilename(String filepath) {
        if (filepath.startsWith("ASSET:")) {
            filepath = filepath.substring(6);
        }
        String[] temp = filepath.split("/");
        return temp[temp.length - 1];
    }

    /**
     * 获取asset目录下文件列表
     *
     * @param path
     * @return
     */
    public static String[] getAssetsFiles(String path) {
        try {
            AssetManager assetManager = context.getAssets();
            return assetManager.list(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String[0];
    }

    /**
     * 读取asset图片
     *
     * @param filepath
     * @return
     */
    public static Drawable readAssetAsDrawable(String filepath) {
        if (filepath.startsWith("ASSET:")) {
            filepath = filepath.substring(6);
        }

        AssetManager assetManager = context.getAssets();
        try {
            InputStream is = assetManager.open(filepath);
            return Drawable.createFromStream(is, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取asset文本文件
     *
     * @param filepath
     * @return
     */
    public static String readAssetAsText(String filepath) {
        if (filepath.startsWith("ASSET:")) {
            filepath = filepath.substring(6);
        }

        AssetManager assetManager = context.getAssets();
        BufferedReader bufferedReader = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open(filepath)));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 读取asset文件
     *
     * @param filepath
     * @return
     */
    public static AssetFileDescriptor readAssetAsFD(String filepath) {
        if (filepath.startsWith("ASSET:")) {
            filepath = filepath.substring(6);
        }

        AssetManager assetManager = context.getAssets();
        try {
            return assetManager.openFd(filepath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取asset文件
     *
     * @param filepath
     * @return
     */
    public static InputStream readAssetAsInputStream(String filepath) {
        if (filepath.startsWith("ASSET:")) {
            filepath = filepath.substring(6);
        }

        AssetManager assetManager = context.getAssets();
        try {
            return assetManager.open(filepath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取文件并映射为key value
     * @param filepath
     * @return
     */
    public static Map<String, String> readAssetAsKeyValue(String filepath){
        Map<String, String> data = new HashMap<>();
        if (filepath.startsWith("ASSET:")) {
            filepath = filepath.substring(6);
        }

        AssetManager assetManager = context.getAssets();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open(filepath)));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                String[] kv = line.split("=");
                if (kv.length == 2) {
                    data.put(kv[0], kv[1]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return data;
    }
}
